package com.my.chapter17;

import net.mindview.util.Countries;

import java.util.LinkedList;

public class practice23 {
    public static void main(String[] args) {
        SimpleHashMap5<String,String>
                m = new SimpleHashMap5<String,String>(),
                m2 = new SimpleHashMap5<String,String>();
        m.putAll(Countries.capitals(10));
        m2.putAll(Countries.capitals(10));
        System.out.println("m.size() = " + m.size());
        System.out.println("m.isEmpty() = " + m.isEmpty());
        System.out.println("m.equals(m2) = " + m.equals(m2));
        System.out.println("m.containsKey(\"BENIN\") = " +
                m.containsKey("BENIN"));
        System.out.println("m.containsKey(\"MARS\") = " +
                m.containsKey("MARS"));
        System.out.println("m.keySet() = " + m.keySet());
        System.out.println("m.values() = " + m.values());
    }
}

class SimpleHashMap5<K,V> extends SimpleHashMap4<K,V> {
    public int size() {
        int sz = 0;
        for(LinkedList<MapEntry<K,V>> bucket : buckets)
            if(bucket != null)
                sz += bucket.size();
        return sz;
    }

    public boolean isEmpty() {
        // Could just say return size() == 0;
        // but this is faster:
        for(LinkedList<MapEntry<K,V>> bucket : buckets)
            if(bucket != null)
                return false;
        return true;
    }

    public boolean containsKey(Object key) {
        for(LinkedList<MapEntry<K,V>> bucket : buckets) {
            if(bucket == null) continue;
            for(MapEntry<K,V> mPair : bucket)
                if(mPair.getKey().equals(key))
                    return true;
        }
        return false;
    }
}
